package com.igeek.car.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.igeek.car.result.Result;
import com.igeek.car.service.PassengersService;
import com.igeek.car.entity.Passengers;
import com.igeek.car.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;

/**
 * OrdersController 订单接口，仅仅只做接口暴露
 * OrdersService    订单业务
 *      OrdersServiceImpl  订单业务的具体实现
 * OrdersMapper     订单的持久层交互
 *
 * Swagger测试地址：http://localhost:8080/swagger-ui.html
 *
 * @author 谢谢谢炎
 * @since 2024-12-05
 */
@RestController
@RequestMapping("/admin/passenger")
@Api(tags = "旅客的接口管理")
public class PassengersController {

    @Autowired
    private PassengersService passengersService;

    // 根据ID获取乘客信息
    @GetMapping("/{passengerId}")
    public Result GetPassengerById(@PathVariable Integer passengerId) {
        Passengers passenger = passengersService.getById(passengerId);
        if (passenger != null) {
            return new Result(true, "成功", passenger);
        } else {
            return new Result(false, "乘客不存在", null);
        }
    }

    // 分页查询乘客信息
    @GetMapping("/page")
    public Result GetPassengersByPage(
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String queryString) {

        // 创建分页对象
        Page<Passengers> page = new Page<>(currentPage, pageSize);

        // 创建条件构造器
        LambdaQueryWrapper<Passengers> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(queryString)) {
            queryWrapper.like(Passengers::getName, queryString)
                    .or()
                    .like(Passengers::getIdCardNumber, queryString);
        }

        // 执行分页查询
        Page<Passengers> passengerPage = passengersService.page(page, queryWrapper);
        return new Result(true, "查询成功", passengerPage);
    }

    // 添加乘客信息
    @PostMapping
    public Result AddPassenger(@RequestBody Passengers passenger) {
        boolean success = passengersService.save(passenger);
        if (success) {
            return new Result(true, "添加成功", passenger);
        } else {
            return new Result(false, "添加失败", null);
        }
    }

    // 更新乘客信息
    @PutMapping("/{passengerId}")
    public Result UpdatePassenger(@PathVariable Integer passengerId, @RequestBody Passengers passenger) {
        passenger.setPassengerId(passengerId);
        boolean success = passengersService.updateById(passenger);
        if (success) {
            return new Result(true, "更新成功", passenger);
        } else {
            return new Result(false, "更新失败", null);
        }
    }

    // 删除乘客信息
    @DeleteMapping("/{passengerId}")
    public Result DeletePassenger(@PathVariable Integer passengerId) {
        boolean success = passengersService.removeById(passengerId);
        if (success) {
            return new Result(true, "删除成功", null);
        } else {
            return new Result(false, "删除失败", null);
        }
    }
}